﻿using AutoMapper;
using ProjectTemplate.Common.Snowflake;
using ProjectTemplate.IRepository;
using ProjectTemplate.IServices;
using ProjectTemplate.Model.DTO.Category;
using ProjectTemplate.Model.Entities;
namespace ProjectTemplate.Services
{
    public class CategoryService : ICategoryService
    {
        private readonly ICategoryRepository _categoryRepository;
        private readonly IMapper _mapper;

        public CategoryService(ICategoryRepository categoryRepository, IMapper mapper)
        {
            _categoryRepository = categoryRepository;
            _mapper = mapper;
        }

        public async Task<long> AddCategory(CategoryAddDTO model)
        {
            var category = new Category
            {
                Id = Snowflake.Id,
                Name = model.Name,
                Sort = model.Sort,
                IsShow = model.IsShow,
                CreateTime = DateTime.Now,
                ImageUrl = model.ImageUrl,
                UpdateTime = DateTime.Now,
            };

            return await _categoryRepository.InsertAsync(category) > 0 ? category.Id : default;
        }

        public async Task<bool> DeleteCategory(long id)
        {
            var result = await _categoryRepository.DeleteByIdAsync(id);

            return result;
        }

        public async Task<List<Category>> GetCategories()
        {
            return await _categoryRepository.QueryAsync();
        }

        public async Task<bool> UpdateCategory(long id, CategoryUpdateDTO dto)
        {
            var category = await _categoryRepository.QueryByIdAsync(id);
            category.Name = dto.Name;
            category.Sort = dto.Sort;
            category.IsShow = dto.IsShow;
            category.ImageUrl = dto.ImageUrl;

            var result = await _categoryRepository.UpdateAsync(category);

            return result;
        }
    }
}
